
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>

#include <unistd.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void test_send_function(int connection_fd);


// build : gcc -Wall -Wshadow -g urg_client.c -o urg_client
int main(int argc, char * argv[]) {

    if (argc < 3) {

        printf("need server ip and port\n");
        exit(1);
    }
    const char * ip = argv[1];
    uint16_t   port = atoi(argv[2]);

    int connection_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (connection_fd < 0) {

        printf("create socket fd failure.\n");
        exit(1);
    }
    struct sockaddr_in sai;
    bzero(&sai, sizeof(sai));

    sai.sin_family = AF_INET;
    sai.sin_port   = htons(port);
    inet_pton(AF_INET, ip, &sai.sin_addr);

    if (connect(connection_fd, (struct sockaddr *)&sai, sizeof(sai)) == 0) {

        // connected
        test_send_function(connection_fd);
    }
    else {

        printf("connect failure");
    }
    close(connection_fd);

    return EXIT_SUCCESS;
}

void test_send_function(int connection_fd) {


    send(connection_fd, "123", 3, 0);
    sleep(1);

    send(connection_fd, "4", 1, MSG_OOB);
    sleep(1);
        
    send(connection_fd, "567", 3, 0);
    sleep(1);

    send(connection_fd, "8911", 4, MSG_OOB);
    sleep(1);

    send(connection_fd, "111213", 6, 0);
    sleep(1);
}
